import G from "./Utilities/Global";
import C from "./Configs/Config";
import BattleScene from "./BattleScene";
import Constants from "./Constants/Constants";
import UIChapter from "./UI/UIChapter";
import UIGameEnd from "./UI/UIGameEnd";

/**
 * 主场景
 * created by Five on 2018-11-8 11:19:25
 */

const { ccclass, property } = cc._decorator;

@ccclass
export default class MainScene extends cc.Component {
    @property(cc.Node)
    logo_shinning: cc.Node = null;

    @property(cc.Node)
    buttons: cc.Node = null;

    @property(cc.Node)
    leadTouch: cc.Node = null;

    onLoad() {
        cc.director.getPhysicsManager().enabled = true;
        //@ts-ignore
        cc.director.getPhysicsManager().debugDrawFlags = cc.PhysicsManager.DrawBits.e_aabbBit |
            //@ts-ignore
            cc.PhysicsManager.DrawBits.e_pairBit |
            //@ts-ignore
            cc.PhysicsManager.DrawBits.e_centerOfMassBit |
            //@ts-ignore
            cc.PhysicsManager.DrawBits.e_jointBit |
            //@ts-ignore
            cc.PhysicsManager.DrawBits.e_shapeBit
            ;
         cc.director.getPhysicsManager().debugDrawFlags = 0;
    }

    start() {
        // 初始化各个大佬
        G().Init();
        C();

        this.buttons.active = false;
        this.node.on(cc.Node.EventType.TOUCH_START, () => {
            this.buttons.active = true;
            this.leadTouch.active = false;
        });

        this.node.on("EnterBattle", () => {
            this.EnterBattle();
        });

        this.leadTouch.runAction(
            cc.repeatForever(
                cc.sequence(
                    cc.fadeOut(1),
                    cc.fadeIn(1)
                )
            ));

        this.logo_shinning.runAction(
            cc.repeatForever(
                cc.sequence(
                    cc.rotateTo(5, 180),
                    cc.rotateTo(5, 360)
                )
            ));

        G().Sound.PlayBGM("login_bgm");
    }

    public ShowChapter() {
        this.loadUI(Constants.PRE_CHAPTER);
        G().Sound.playSFX("btn_clicked");
    }

    private async loadUI(uiName: string) {
        let node = await G().Res.GetRes(uiName) as cc.Node;
        node.position = cc.Vec2.ZERO;
        this.node.addChild(node);
        return node;
    }

    public async EnterStory() {
        let node = await G().Res.GetRes(Constants.PRE_OPENING) as cc.Node;
        this.node.addChild(node);
        G().Sound.playSFX("btn_clicked");
    }

    public EnterBattle() {
        G().ToBattleScene(G().SelectedMap, false);
    }

    public EditMode() {
        G().ToBattleScene(-1, true);
        G().Sound.playSFX("btn_clicked");
    }

    test: boolean = false;
    async FeatureTest() {
        G().Setting.Open(1);
    }
}
